package com.wtwd.campus.adapter;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.VerifyResult;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author mjy
 * @Date create in 9:19 2022/3/9
 * @Description
 */
public class H5ManageAdapter extends VerifyAdapter {

    public H5ManageAdapter(HttpServletRequest request) {
        super(request);
        encryptVerify = null;
    }

    @Override
    public VerifyResult verifyEncrypt(HttpServletRequest request, RedisService redisService) {
        if (encryptVerify == null) {
            VerifyResult verifyResult = new VerifyResult();
            if (StringUtils.isBlank(h5MangeToken)) {
                verifyResult.setResult(false);
                verifyResult.setRespCode(RespCode.TOKEN_UNAUTHORIZED);
                return verifyResult;
            }
            // 获取token对应的东西
            Map<Object, Object> userMsg = redisService.getUserMsgByToken(h5MangeToken);
            if (userMsg.isEmpty()) {
                verifyResult.setResult(false);
                verifyResult.setRespCode(RespCode.TOKEN_UNAUTHORIZED);
                return verifyResult;
            }
            String currentToken = (String) userMsg.get("current_token");
            if (!h5MangeToken.equals(currentToken)) {
                verifyResult.setResult(false);
                verifyResult.setRespCode(RespCode.TOKEN_CONFLICT);
                return verifyResult;
            }
            return null;
        }
        return null;
    }
}
